#include "LedController.h"
#include "PwmOut.h"
#include "SW1248.h"
#include "CmdLine.h"

#include "DataStorage.h"
#include "log.h"
#include "DataStorageAddressableLed.h"

LedController::LedController(I2CSlave& i2c_slave,DataStorage& dataStorage,Multiplexers &multiplexers):
    _i2c_slave(i2c_slave),
    _dataStorage(dataStorage),
    _multiplexers(multiplexers)
{
    _isr_update = false;
}

LedController::~LedController()
{
    
}
void LedController::begin()
{
    _multiplexers.close();
}

void LedController::schedule()
{
    if(_isr_update){
        return;
    }

    if(!_i2c_slave.read(_reg_data_read)){
        return;
    }
   
   this->update();
}
void LedController::update()
{
    module_general_reg_t& general_reg = AddressableLedData::module_reg().module_general_reg;
    
    log::debug("mem:%x:%x:%x-%x:%x:%x\r\n",
                _reg_data_read.memory.data[0],
                _reg_data_read.memory.data[1],
                _reg_data_read.memory.data[2],
                _reg_data_read.memory.data[3],
                _reg_data_read.memory.data[4],
                _reg_data_read.memory.data[5]);
    switch(_reg_data_read.memory.data[0]){
        case 0x41://保留
        break;
        case 0x42://更新设置
        {
            //软件自动清零
            // module_control_reg_t& module_control_reg = general_reg.module_control_reg;
        }
        break;
        case 0x43://保留
        break;
        case 0x44://保留
        break;
        case 0x45://保留
        break;
        case 0x46://保留
        break;
        case 0x47://保留
        break;
        case 0x48:
        {   
            // 0: 关闭
            // 1: 使能
            module_mux_enable_reg_t& enable_reg = general_reg.module_mux_enable_reg;
            enable_reg.value = _reg_data_read.memory.data[1];
            _multiplexers.mux0().enable( (enable_reg.pin.mux0 == 1)?true:false);
            _multiplexers.mux1().enable( (enable_reg.pin.mux1 == 1)?true:false);
            _multiplexers.mux2().enable( (enable_reg.pin.mux2 == 1)?true:false);
            log::debug("mux0:%d-mux1:%d-mux2:%d\r\n",enable_reg.pin.mux0,enable_reg.pin.mux1,enable_reg.pin.mux2);
        }
        break;
        case 0x49://保留
        break;
        case 0x4A:
        {
            
            // 0: 断开公共端
            // 1: 选通公共端
            general_reg.module_mux0_channel_select_reg.data[0]  = _reg_data_read.memory.data[1];

            switch(general_reg.module_mux0_channel_select_reg.value){
                case 0b00000001:_multiplexers.mux0().select_channel(ti::CD74HC4051::ChannelName::A0);break;
                case 0b00000010:_multiplexers.mux0().select_channel(ti::CD74HC4051::ChannelName::A1);break;
                case 0b00000100:_multiplexers.mux0().select_channel(ti::CD74HC4051::ChannelName::A2);break;
                case 0b00001000:_multiplexers.mux0().select_channel(ti::CD74HC4051::ChannelName::A3);break;
                case 0b00010000:_multiplexers.mux0().select_channel(ti::CD74HC4051::ChannelName::A4);break;
                case 0b00100000:_multiplexers.mux0().select_channel(ti::CD74HC4051::ChannelName::A5);break;
                case 0b01000000:_multiplexers.mux0().select_channel(ti::CD74HC4051::ChannelName::A6);break;
                case 0b10000000:_multiplexers.mux0().select_channel(ti::CD74HC4051::ChannelName::A7);break;
                default:_multiplexers.mux0().enable(false);break;
            }
            log::debug("4a-write:%d\r\n",general_reg.module_mux0_channel_select_reg.value);
        }
        break;
        case 0x4B:
        {   
            // 0: 断开公共端
            // 1: 选通公共端
            general_reg.module_mux1_channel_select_reg.data[0]  = _reg_data_read.memory.data[1];
            switch(general_reg.module_mux1_channel_select_reg.value){
                case 0b00000001:_multiplexers.mux1().select_channel(ti::CD74HC4051::ChannelName::A0);break;
                case 0b00000010:_multiplexers.mux1().select_channel(ti::CD74HC4051::ChannelName::A1);break;
                case 0b00000100:_multiplexers.mux1().select_channel(ti::CD74HC4051::ChannelName::A2);break;
                case 0b00001000:_multiplexers.mux1().select_channel(ti::CD74HC4051::ChannelName::A3);break;
                case 0b00010000:_multiplexers.mux1().select_channel(ti::CD74HC4051::ChannelName::A4);break;
                case 0b00100000:_multiplexers.mux1().select_channel(ti::CD74HC4051::ChannelName::A5);break;
                case 0b01000000:_multiplexers.mux1().select_channel(ti::CD74HC4051::ChannelName::A6);break;
                case 0b10000000:_multiplexers.mux1().select_channel(ti::CD74HC4051::ChannelName::A7);break;
                default:_multiplexers.mux1().enable(false);break;
            }
            log::debug("4b-write:%d\r\n",general_reg.module_mux1_channel_select_reg.value);
        }
        break;
        case 0x4C:
        {
            // 0: 断开公共端
            // 1: 选通公共端
            general_reg.module_mux2_channel_select_reg.data[0] = _reg_data_read.memory.data[1];
            switch(general_reg.module_mux2_channel_select_reg.value){
                case 0b00000001:_multiplexers.mux2().select_channel(ti::CD74HC4051::ChannelName::A0);break;
                case 0b00000010:_multiplexers.mux2().select_channel(ti::CD74HC4051::ChannelName::A1);break;
                case 0b00000100:_multiplexers.mux2().select_channel(ti::CD74HC4051::ChannelName::A2);break;
                case 0b00001000:_multiplexers.mux2().select_channel(ti::CD74HC4051::ChannelName::A3);break;
                case 0b00010000:_multiplexers.mux2().select_channel(ti::CD74HC4051::ChannelName::A4);break;
                case 0b00100000:_multiplexers.mux2().select_channel(ti::CD74HC4051::ChannelName::A5);break;
                case 0b01000000:_multiplexers.mux2().select_channel(ti::CD74HC4051::ChannelName::A6);break;
                case 0b10000000:_multiplexers.mux2().select_channel(ti::CD74HC4051::ChannelName::A7);break;
                default:_multiplexers.mux2().enable(false);break;
            }
            log::debug("4c-write:%d\r\n",general_reg.module_mux2_channel_select_reg.value);
        }
        break;
        default:break;
    }
}
void LedController::cleanup()
{

}


